package com.itwn.controller;

import com.github.pagehelper.PageInfo;
import com.itwn.entity.Movie;
import com.itwn.entity.UrlPermission;
import com.itwn.result.ResponseResult;
import com.itwn.service.MovieService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@Api
@RequestMapping("MovieMudule")
public class MovieController {

    @Autowired
    private MovieService movieService;

    @GetMapping("/list")
    @ApiOperation(value = "查询电影列表")
    public ResponseResult<List<Movie>> findAllMovies(){
        ResponseResult<List<Movie>> result = new ResponseResult<>();
        List<Movie> movieList = movieService.findAllMovies();
        if(movieList.size()>0){
            result.setCode(200);
            result.setMsg("查询电影列表成功");
            result.setData(movieList);
        }else{
            result.setCode(201);
            result.setMsg("查询失败");
        }
        return result;
    }

    @GetMapping("/MovieList")
    @ApiOperation(value = "电影列表显示")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    paramType="query",name="pageNum",value="分页参数的当前页",
                    dataType="int",required=false),
            @ApiImplicitParam(
                    paramType="query",name="pageSize",value="分页参数的每页查询条数",
                    dataType="int",required=false),
            @ApiImplicitParam(
                    paramType="query",name="movieId",value="电影ID",
                    dataType="string",required=false),
            @ApiImplicitParam(
                    paramType="query",name="movieName",value="电影名称",
                    dataType="String",required=false)
    })
    public PageInfo<Movie> findWithPage(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                                        String text,String movieId, String movieName){
        boolean b = text.matches("\\d+");
        if(b){
            movieId=text;
        }else {
            movieName=text;
        }

        PageInfo<Movie> pageInfo = movieService.findMoviePage(pageNum, pageSize,movieName,movieId);

        return pageInfo;
    }

    @PostMapping("/movieAdd")
    @ApiOperation(value = "添加电影")
    public ResponseResult<Movie> add( Movie movie){
        ResponseResult<Movie> result = new ResponseResult<>();
        int i= movieService.addMovie(movie);
        if (i == 1){
            result.setCode(200);
            result.setData(movie);
            return result ;
        }else {
            result.setCode(101);
            return result;
        }
    }

    @PostMapping("/updateMovie")
    @ApiOperation(value = "修改电影")
    public ResponseResult<Movie> updatePer( Movie movie){

        ResponseResult<Movie> result = new ResponseResult<>();
        int i= movieService.updateMovie(movie);
        if (i == 1){
            result.setCode(200);
            result.setData(movie);
            return result ;
        }else {
            result.setCode(101);
            return result;
        }
    }

    @PostMapping("/deleteMovie")
    @ApiOperation(value = "删除电影")
    @ApiImplicitParam(
            paramType = "delete",name="id",value = "权限的id",
            dataType = "int",required = true)
    public ResponseResult<UrlPermission> removePermission(@RequestParam(value = "id") int id){
        ResponseResult<UrlPermission> result = new ResponseResult<>();
        int i= movieService.removeById(id);
        if (i == 1){
            result.setCode(200);
            return result ;
        }else {
            result.setCode(101);
            return result;
        }
    }

}
